<?php

namespace App\HttpController\Api;

use App\Think\Org\Ems as Emslib;
use App\Think\Validate;
use App\Think\Config;

// use App\Think\Org\Email;
/**
 * 邮箱验证码接口Ems
 */
class Ems extends ApiBase
{
    protected $noNeedLogin = ['*'];

    /**
     * 发送验证码
     * ApiMethod (POST)
     * @param string $email 邮箱
     * @param string $event 事件名称:register=注册,resetpwd=重置密码,changeemail=修改邮箱
     * @param string $captcha 图形验证码|event=resetpwd时,必选
     * @param string $captcha_id 图形验证码ID
     */
    public function send()
    {
        $email      = $this->input("email");
        $event      = $this->input("event");
        $captcha    = $this->input("captcha");
        $captcha_id = $this->input("captcha_id");

        $event = $event ? $event : 'register';
        if (!Validate::is($email, "email")) return $this->error(__('邮箱格式不正确'));
        if(in_array($event, ['resetpwd'])){
            $cls = new \App\Think\Captcha();
            if(!$cls->captcha_check($captcha, $captcha_id)) return $this->error(__('图形验证码不正确'));
            $user = db_get_row('user', "email='{$email}'");
            if (!$user) return $this->error(__('该邮箱地址未被绑定'));
        }
        $last = Emslib::get($email, $event);
        // if ($last && time() - $last['createtime'] < 60) return $this->error(__('发送频繁,请稍后再试'));
        if ($event) {
            $userinfo = db_get_row('user', "email='{$email}'");
            if ($event == 'register' && $userinfo) {
                //已被注册
                return $this->error(__('已被注册'));
            } elseif (in_array($event, ['changeemail']) && $userinfo) {
                //被占用
                return $this->error(__('当前邮箱已被注册'));
            } elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
                //未注册
                return $this->error(__('未注册'));
            }
        }
        $ret = Emslib::send($email, null, $event);
        if ($ret) {
            return $this->success(__('发送成功,请登录邮箱查看'));
        } else {
            return $this->error(__('发送失败,请检查邮箱配置'));
        }
    }

    /**
     * 检测验证码
     * ApiMethod (POST)
     * @param string $email   邮箱
     * @param string $event   事件名称
     * @param string $captcha 验证码
     */
    public function check()
    {
        $email   = $this->input("email");
        $event   = $this->input("event");
        $captcha = $this->input("captcha");

        $event = $event ? $event : 'register';
        if ($event) {
            $userinfo = db_get_row('user', "email='{$email}'");
            if ($event == 'register' && $userinfo) {
                //已被注册
                return $this->error(__('已被注册'));
            } elseif (in_array($event, ['changeemail']) && $userinfo) {
                //被占用
                return $this->error(__('已被占用'));
            } elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
                //未注册
                return $this->error(__('未注册'));
            }
        }
        $ret = Emslib::check($email, $captcha, $event);
        if ($ret) {
            return $this->success(__('success'));
        } else {
            return $this->error(__('验证码不正确'));
        }
    }
}
